<template>
  <div class="login-container">
    <div class="logo" />
    <div class="form">
      <h1>登录</h1>
      <!-- el-card卡片组件，提供基本的阴影效果 -->
      <el-card shadow="never" class="login-card">

        <!--登录表单-->
        <!-- 1.表单校验是整体性校验 必须给el-form提供一整个对象数据model -->
        <!-- 2.rules是校验规则，ref通信 -->
        <el-form ref="loginForm" :model="obj" :rules="rules">

          <!-- 3.必须给当前行el-form-item 添加一个prop 指定验证规则字段名字 -->
          <!-- 4.必须双绑，收集数据 -->
          <el-form-item prop="mobile">
            <el-input v-model="obj.mobile" placeholder="请输入账号" />
          </el-form-item>

          <el-form-item prop="password">
            <el-input v-model="obj.password" placeholder="请输入密码" :show-password="true" />
          </el-form-item>

          <el-form-item prop="isAgree">
            <el-checkbox v-model="obj.isAgree">用户使用协议</el-checkbox>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" style="width: 350px;" @click="loginForm">登录</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>
<script>
export default {
  name: 'Login',

  data() {
    return {
      obj: {
        // process.env.NODE_ENV 环境变量
        // mobile: '13800000002',
        mobile: process.env.NODE_ENV === 'development' ? '13800000002' : '',
        // password: 'hm#qd@23!',
        password: process.env.NODE_ENV === 'development' ? 'hm#qd@23!' : '',
        // isAgree: false
        isAgree: process.env.NODE_ENV === 'development'
      },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          // 用户手机号正则规则
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式有问题', trigger: 'blur' }

        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          // 密码正则规则
          { min: 6, max: 16, message: '密码必须6-16位数', trigger: 'blur' }
        ],
        isAgree: [
          {
            // validator会自动在失焦时执行
            validator(rule, value, callback) {
              // value就是用户输入的值
              // callback是个函数，希望校验通过，直接调用函数即可；反之，调用函数时传递一个错误即可
              if (!value) {
                return callback(new Error('请勾选协议'))
              } else {
                callback()
              }
            },
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    loginForm() {
      this.$refs.loginForm.validate(async(isOk) => {
        // isOK表示校验是否通过
        // console.log(isOk)
        if (isOk) {
          // 通过：发送请求 => 调用准备好的action
          await this.$store.dispatch('user/login', this.obj)
          // 等登陆完成后，跳转
          this.$router.push('/')
          // 提示
          this.$message.success('登录成功')
        }
      })
    }
  }
}
</script>
<style lang="scss">
.login-container {
  display: flex;
  align-items: stretch;
  height: 100vh;
  .logo {
    flex: 3;
    background: rgba(38, 72, 176) url(../../assets/common/login_back.png)
      no-repeat center / cover;
    border-top-right-radius: 60px;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    justify-content: center;
    padding: 0 100px;
    .icon {
      background: url(../../assets/common/logo.png) no-repeat 70px center /
        contain;
      width: 300px;
      height: 50px;
      margin-bottom: 50px;
    }
    p {
      color: #fff;
      font-size: 18px;
      margin-top: 20px;
      width: 300px;
      text-align: center;
    }
  }
  .form {
    flex: 2;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding-left: 176px;
    .el-card {
      border: none;
      padding: 0;
    }
    h1 {
      padding-left: 20px;
      font-size: 24px;
    }
    .el-input {
      width: 350px;
      height: 44px;
      .el-input__inner {
        background: #f4f5fb;
      }
    }
    .el-checkbox {
      color:#606266;
    }
  }
}
</style>
